import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { DefectDefineModel } from '#/api/quality/models';
import type { DictModel } from '#/api/system/models';

import { getDictByCodeApi } from '#/api/system/dict';

const _mapping: Record<string, string> = {};

/**
 * 获取表格查询表单的字段配置。
 */
export function useSearchSchema(): VbenFormSchema[] {
  return [
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        afterFetch: (data: DictModel) => {
          return data.items.map((item) => ({
            label: item.label,
            value: item.code,
          }));
        },
        api: getDictByCodeApi,
        params: { code: 'mes_quality_defect_type' },
        immediate: false,
      },
      fieldName: 'defectType',
      label: '缺陷类型',
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'defectCode',
      label: '缺陷代码',
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'defectName',
      label: '缺陷名称',
    },
  ];
}

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'defectCode',
      label: '缺陷代码',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'defectName',
      label: '缺陷名称',
      rules: 'required',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        afterFetch: (data: DictModel) => {
          data.items.forEach((item) => {
            _mapping[item.code] = item.label;
          });
          return data.items.map((item) => ({
            label: item.label,
            value: item.code,
          }));
        },
        api: getDictByCodeApi,
        params: { code: 'mes_quality_defect_type' },
      },
      dependencies: {
        trigger(values, form) {
          form.setFieldValue('defectTypeDesc', _mapping[values.defectType]);
        },
        triggerFields: ['defectType'],
      },
      fieldName: 'defectType',
      label: '缺陷分类',
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: '轻微', value: 1 },
          { label: '一般', value: 2 },
          { label: '严重', value: 3 },
          { label: '致命', value: 4 },
        ],
      },
      fieldName: 'defectLevel',
      label: '严重等级',
      rules: 'required',
      defaultValue: 1,
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 1,
        precision: 0,
      },
      fieldName: 'order',
      label: '排序',
      defaultValue: 100,
      rules: 'required',
    },
    {
      component: 'Switch',
      componentProps: {
        checkedValue: 1,
        unCheckedValue: 0,
        checkedChildren: '启用',
        unCheckedChildren: '禁用',
        class: 'w-auto',
      },
      fieldName: 'status',
      label: '状态',
      defaultValue: 1,
      rules: 'required',
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 128,
      },
      fieldName: 'defectDesc',
      label: '缺陷描述',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<DefectDefineModel>,
): VxeTableGridOptions<DefectDefineModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'defectCode', title: '缺陷代码', width: 140 },
    { field: 'defectName', title: '缺陷名称', width: 140 },
    { field: 'defectTypeDesc', title: '缺陷类型', width: 100 },
    {
      field: 'defectLevel',
      title: '严重等级',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'lime', label: '轻微', value: 1 },
          { color: 'warning', label: '一般', value: 2 },
          { color: 'red', label: '严重', value: 3 },
          { color: 'error', label: '致命', value: 4 },
        ],
      },
    },
    { field: 'order', title: '排序', width: 80 },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: { name: 'CellTag', attrs: { useStatus: true } },
    },
    { field: 'defectDesc', title: '缺陷描述', minWidth: 100 },
    { field: 'createdTime', title: '创建时间', width: 160 },
    {
      cellRender: {
        attrs: {
          nameField: 'machineryName',
          nameTitle: '设备',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          'edit', // 默认的编辑按钮
          'delete', // 默认的删除按钮
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 120,
    },
  ];
}
